package com.questetra.bpms.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.api.client.auth.oauth.OAuthCredentialsResponse;

public class Preferences {
    private static final String PREFERENCES_NAME = "OAuth";
    private static final String PREFERENCES_HOST = "host";
    private static final String PREFERENCES_PATH = "url";
    private static final String PREFERENCES_CONSUMER_SECRET = "consumerSecret";
    private static final String PREFERENCES_TOKEN = "token";
    private static final String PREFERENCES_TOKEN_SECRET = "tokenSecret";

    public static SharedPreferences getSharedPreferences(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME,
                                                                     Context.MODE_PRIVATE);
        return preferences;
    }

    public static QbpmsInfo getQbpmsInfo(Context context)
            throws NotFoundException {
        SharedPreferences preferences = getSharedPreferences(context);
        if (preferences.contains(PREFERENCES_PATH)
                && preferences.contains(PREFERENCES_CONSUMER_SECRET)) {
            QbpmsInfo info = new QbpmsInfo();
            info.host = preferences.getString(PREFERENCES_HOST, "s");
            info.path = preferences.getString(PREFERENCES_PATH, "");
            info.consumerKey = OAuthService.CONSUMER_KEY;
            info.consumerSecret = preferences.getString(PREFERENCES_CONSUMER_SECRET,
                                                        "");
            return info;
        }
        throw new NotFoundException();
    }

    public static void putQbpmsInfo(Context context, QbpmsInfo info) {
        SharedPreferences preferences = getSharedPreferences(context);

        Editor editor = preferences.edit();
        editor.putString(PREFERENCES_HOST, info.host);
        editor.putString(PREFERENCES_PATH, info.path);
        editor.putString(PREFERENCES_CONSUMER_SECRET, info.consumerSecret);

        // 接続先情報を変更する場合は、自動的にトークン情報を削除する。
        editor.remove(PREFERENCES_TOKEN);
        editor.remove(PREFERENCES_TOKEN_SECRET);
        editor.commit();
    }

    public static void removeQbpmsInfo(Context context) {
        SharedPreferences preferences = getSharedPreferences(context);

        Editor editor = preferences.edit();
        editor.remove(PREFERENCES_PATH);
        editor.remove(PREFERENCES_CONSUMER_SECRET);

        editor.remove(PREFERENCES_TOKEN);
        editor.remove(PREFERENCES_TOKEN_SECRET);
        editor.commit();
    }

    public static OAuthCredentialsResponse getToken(Context context)
            throws NotFoundException {
        SharedPreferences preferences = getSharedPreferences(context);
        if (preferences.contains(PREFERENCES_TOKEN)
                && preferences.contains(PREFERENCES_TOKEN_SECRET)) {
            OAuthCredentialsResponse credentials = new OAuthCredentialsResponse();
            credentials.token = preferences.getString(PREFERENCES_TOKEN, "");
            credentials.tokenSecret = preferences.getString(PREFERENCES_TOKEN_SECRET,
                                                            "");
            return credentials;
        }
        throw new NotFoundException();
    }

    public static void putToken(Context context,
                                OAuthCredentialsResponse credentials) {
        SharedPreferences preferences = getSharedPreferences(context);

        Editor editor = preferences.edit();
        editor.putString(PREFERENCES_TOKEN, credentials.token);
        editor.putString(PREFERENCES_TOKEN_SECRET, credentials.tokenSecret);
        editor.commit();
    }

    public static void removeToken(Context context) {
        SharedPreferences preferences = getSharedPreferences(context);

        Editor editor = preferences.edit();
        editor.remove(PREFERENCES_TOKEN);
        editor.remove(PREFERENCES_TOKEN_SECRET);
        editor.commit();
    }

    public static class NotFoundException extends Exception {
        private static final long serialVersionUID = 1L;
    }
}